/**
* File Name: pthread_exit.c
* Auther: Jichang
* Environment: Fedora 7 server
* test pthread_exit function.
*/
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<pthread.h>

#define N 5  // number of child-thread.

void sys_err(const char *str) 
{
	perror(str);
	exit(-1);
}

void func(void)
{
	pthread_exit(NULL); // quit current thread.
	return;
}

// call back
void *t_func(void *arg)
{
	short i = (int)arg;  // use i to this function.
	sleep(i);

	// not quit child thread, but quit process.
	//if(i == 1) {
		// exit(0); 
		// return; // return function caller.
		//func();
	//}
	printf("%d thread: pid=%d, tid=%lu\n", i+1, getpid(), pthread_self());
	return NULL;
}

int main(int argc, char *argv[])
{
	int i, ret;
	pthread_t tid;
	for(i = 0; i < N; i++) {
		ret = pthread_create(&tid, NULL, t_func, (void *)i); // Give i.
		if(ret != 0) {
			sys_err("pthread_create error");
		}
	}
	// sleep(i);
	printf("main: pid=%d, tid=%lu\n", getpid(), pthread_self());
	pthread_exit((void*)0); // quit this child
	
	return 0;
}

